﻿@model CategoryNavigationModel
@using Nop.Web.Models.Catalog;

@functions{
    public bool BreadCrumbContainsCurrentCategoryId(CategorySimpleModel category)
    {
        if (Model.CurrentCategoryId == 0)
            return false;

        if (category.Id == Model.CurrentCategoryId)
            return true;

        foreach (var subCategory in category.SubCategories)
        {
            if (BreadCrumbContainsCurrentCategoryId(subCategory))
            {
                return true;
            }
        }

        return false;
    }
}
@helper RenderCategoryLine(CategorySimpleModel category)
{
    <li class="@(category.Id == Model.CurrentCategoryId ? "active" : "inactive")">
        <a href="@Url.RouteUrl("Category", new { SeName = category.SeName })">@category.Name
            @if (category.NumberOfProducts.HasValue)
            {
                <text> </text>@T("Categories.TotalProducts", category.NumberOfProducts.Value)
            }
        </a>
        @{
            if (category.Id == Model.CurrentCategoryId ||
                category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0)
            {
                if (category.SubCategories.Count > 0)
                {
            <ul class="sublist">
                @foreach (var subCategory in category.SubCategories)
                {
                    @RenderCategoryLine(subCategory)
                }
            </ul>
                }
            }
        }
    </li>
}
@if (Model.Categories.Count > 0)
{
    <div class="block block-category-navigation">
        <div class="title">
            <strong>@T("Categories")</strong>
        </div>
        <div class="listbox">
            <ul class="list">
                @foreach (var category in Model.Categories)
                {
                    @RenderCategoryLine(category)
                }
            </ul>
        </div>
    </div>
}